package com.ellricky.baselib.ui.teacher

import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.andr.common.util.ResUtil
import com.andr.common.widget.tabvp.SimpleTabLayout
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.StaffInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.ui.OverviewRankFragment
import com.ellricky.baselib.ui.base.BaseDetailActivity
import com.ellricky.baselib.ui.overdata.OverviewConsultantDataFragment
import com.ellricky.baselib.ui.overdata.OverviewMarketDataFragment
import com.ellricky.baselib.ui.overdata.OverviewTeacherDataFragment
import com.ellricky.baselib.ui.overdata.OverviewTmDataFragment
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import de.hdodenhof.circleimageview.CircleImageView
import java.util.*

/**
 * 员工详情-学管/课程顾问
 */
class EmployeeDetailActivity : BaseDetailActivity() {
    private var ivAvatar: CircleImageView? = null
    private var tvName: TextView? = null
    private var tvWorkDate: TextView? = null
    private var tvTel: TextView? = null
    private var tvBirth: TextView? = null
    private var tabLayout: SimpleTabLayout? = null
    private var vp: ViewPager? = null
    private var keyList: List<String>? = null
    private var frgList: MutableList<Fragment>? = null

    private val cardType: String by lazy {
        intent.getStringExtra("card_type") ?: ""
    }

    private val id: String by lazy {
        intent.getStringExtra("id") ?: ""
    }

    override fun getContentLayout(): Int {
        return R.layout.base_activity_tc_detail
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.setText(R.string.base_employee_detail)
        ivAvatar = findViewById(R.id.ivAvatar)
        tvName = findViewById(R.id.tvName)
        tvWorkDate = findViewById(R.id.tvWorkDate)
        tvTel = findViewById(R.id.tvTel)
        tvBirth = findViewById(R.id.tvBirth)
        vp = findViewById(R.id.vp)
        vp?.offscreenPageLimit = 3
        tabLayout = findViewById(R.id.tabLayout)
        tvName?.setText(getString(R.string.base_name_str, ""))
        tvWorkDate?.setText(getString(R.string.base_work_date_str, ""))
        tvTel?.setText(getString(R.string.base_tel_str, ""))
        tvBirth?.setText(getString(R.string.base_birth_str, ""))
        keyList = Arrays.asList(*ResUtil.getStrArr(this, R.array.TcGridItem))
        frgList = ArrayList()
        frgList?.add(getCardTypeFragment())
        frgList?.add(OverviewAchievementFragment())
        frgList?.add(OverviewRankFragment())
        val tList = Arrays.asList(*ResUtil.getStrArr(this, R.array.TcItem))
        tabLayout?.setUp(supportFragmentManager, frgList, tList, vp)
        loadDataOnInit()
    }

    fun getCardTypeFragment(): Fragment {
        when (cardType) {
            BaseConstant.CARD_TEACHER_MANAGER -> {
                return OverviewTmDataFragment(id)
            }
            BaseConstant.CARD_TEACHER -> {
                return OverviewTeacherDataFragment(id)
            }
            BaseConstant.CARD_CONSULTANT -> {
                return OverviewConsultantDataFragment(id)
            }
            BaseConstant.CARD_MARKET -> {
                return OverviewMarketDataFragment(id)
            }
        }
        return Fragment()
    }

    private fun loadDataOnInit() {
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf(
            "action" to "empDetail",
            "eid" to LocalCacheUtil.getUid(),
            "emp_id" to LocalCacheUtil.getUid(),
        )

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<StaffInfo>(this) {
                override fun onSuccess(data: StaffInfo, msg: String) {
                    tvName?.text = "姓名：${data.E_Name}"
                    tvTel?.text = "联系方式：${data.E_Tel}"
                    tvWorkDate?.text = "入职时间：${data.E_CreatTime}"
                    tvBirth?.text = "生日：${data.E_birthday}"
                }
            })
    }
}